using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseTank : MonoBehaviour
{
    protected GameObject tank;

    public int steer = 20;
    public int speed = 3;

    public float fireCd = 0.5f;
    public float lastFireTime;

    public float hp = 100;

    public int turretSpeed = 30;
    public Transform turret;
    public Transform muzzle;
    public Transform firePoint;

    protected Rigidbody rb;
    protected BoxCollider bc;
    public virtual void Init(string tankPath)
    {
        var tankRes = ResManager.LoadPrefab(tankPath);
        tank = Instantiate(tankRes);
        tank.transform.parent = transform;
        tank.transform.localPosition = Vector3.zero;
        tank.transform.localEulerAngles = Vector3.zero;

        rb = gameObject.AddComponent<Rigidbody>();
        bc= gameObject.AddComponent<BoxCollider>();
        bc.center = new Vector3(0, 2.5f, 1.5f);
        bc.size = new Vector3(7, 5, 12);

        turret = tank.transform.Find("turret");
        muzzle = turret.transform.Find("muzzle");
        firePoint = muzzle.transform.Find("FirePoint");
    }

    public Bullet BulletFire()
    {
        if (IsDie())
        {
            return null;
        }
        var bulletObj = new GameObject("bullet");
        Bullet bullet = bulletObj.AddComponent<Bullet>();
        bullet.Init();
        bullet.tank = this;
        
        bullet.transform.position = firePoint.position;
        bullet.transform.rotation = firePoint.rotation;
        
        lastFireTime = Time.time;
        return bullet;
    }
    public bool IsDie()
    {
        return hp <= 0;
    }
    public void Attacked(float att)
    {
        if (IsDie())
        {
            return;
        }
        hp -= att;
        if (IsDie())
        {
            //var obj = ResManager.LoadPrefab("explosion");
            //var explosion = Instantiate(obj, transform.position, transform.rotation);
            //explosion.transform.SetParent(transform);
        }
    }
}
